<template>
    <div class="e-form">
        <BillTop title="外协台账新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData" label-width="200px"  :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="外协单位：" prop="supplierName">
                            <el-input v-model="formData.supplierName" readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectUnit"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="设备名称：" prop="equipmentName">
                            <el-input v-model="formData.equipmentName" readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectEquip"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                     <el-col :span="12">
                        <el-form-item label="规格型号："  prop="specificationModel">
                            <el-input v-model="formData.specificationModel"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="计量单位：">
                            <span>{{ formData.unit }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="操作手：">
                            <el-input v-model="formData.operatorName"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="进场时间：" prop="enterDate">
                            <el-date-picker
                                v-model="formData.enterDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="退场时间："  prop="exitDate">
                            <el-date-picker
                                v-model="formData.exitDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                   <el-col :span="12">
                        <el-form-item label="使用地点：" prop="useSite">
                            <el-input v-model="formData.useSite"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="外协设备来源：" prop="equipmentSource">
                            <el-select v-model="formData.equipmentSource">
                                <el-option :value="item.value"  :label="item.label" v-for="item,index in equipmentSource" :key="index" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                   <el-col :span="12">
                         <el-form-item label="出租/销售单位：">
                            <el-input v-model="formData.leaseSaleUnit"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="已付租赁/设备费（元）：" prop="paid">
                            <el-input v-model="formData.paid"
                            v-enter-float="{
                                set: this,
                                val: 'formData',
                                key: 'paid'
                            }"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                   <el-col :span="12">
                         <el-form-item label="车牌编号：" prop="plateNo">
                            <el-input v-model="formData.plateNo"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="未付租赁/设备费（元）：" prop="unpaid">
                            <el-input v-model="formData.unpaid"
                            v-enter-float="{
                                set: this,
                                val: 'formData',
                                key: 'unpaid'
                            }"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="进退场状态：" prop="enterExitState">
                            <el-select v-model="formData.enterExitState">
                                <el-option :value="item.value"  :label="item.label" v-for="item,index in enterExitStatus" :key="index" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                         <el-form-item label="所属机构：">
                            <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/equipAccount.js'
import { mapState } from 'vuex'
import { selectDataClient } from '@/utils/common'
import { selectOutsourceUnit } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            formData: {
                'billId': '',
                'categoryId': '',
                'categoryName': '',
                'enrollDate': '',
                'enterDate': '',
                'enterExitState': null,
                'equipmentCondition': '',
                'equipmentId': '',
                'equipmentName': '',
                'equipmentNo': '',
                'equipmentSource': '',
                'exitDate': '',
                'founderId': '',
                'founderName': '',
                'isBack': true,
                'leaseAccountId': '',
                'leaseSaleUnit': '',
                'operatorName': '',
                'orgId': '',
                'orgName': '',
                'paid': null,
                'plateNo': '',
                'shortCode': '',
                'specificationModel': '',
                'supplierId': '',
                'supplierName': '',
                'unit': '',
                'unpaid': null,
                'useSite': '',
                'contractId': ''
            },
            rules: {
                supplierName: [
                    { required: true, message: '请选择外协单位', trigger: 'change' },
                ],
                equipmentName: [
                    { required: true, message: '请选择设备名称', trigger: 'change'  },
                ],
                enterDate: [
                    { required: true, message: '请选择进场时间', trigger: 'change'  },
                    { required: true, validator: (rule, value, callback)=>{
                        if (this.formData.exitDate) {
                            const enterDate = new Date(this.formData.enterDate).getTime() //进场时间
                            const exitDate = new Date(this.formData.exitDate).getTime() //退场时间
                            if(enterDate > exitDate) {
                                callback(new Error('进场时间不能大于退场时间'))
                            }else{
                                callback()
                            }
                        }else{
                            callback()
                        }
                    }, trigger: ['blur', 'change'] }
                ],
                exitDate: [
                    { required: false, validator: (rule, value, callback)=>{
                        if (this.formData.enterDate) {
                            const enterDate = new Date(this.formData.enterDate).getTime() //进场时间
                            const exitDate = new Date(this.formData.exitDate).getTime() //退场时间
                            if(enterDate > exitDate) {
                                callback(new Error('退场时间不能小于进场时间'))
                            }else{
                                callback()
                            }
                        }else{
                            callback()
                        }
                    }, trigger: ['blur', 'change'] }
                ],
                useSite: [
                    { required: true, message: '请填写使用地点', trigger: 'blur'  },
                ],
                equipmentSource: [
                    { required: true, message: '请选择外协设备来源', trigger: 'change'  },
                ],
                paid: [
                    { required: true, message: '请填写已付租赁/设备费（元）', trigger: 'blur'  },
                ],
                plateNo: [
                    { required: true, message: '请填写车牌号', trigger: 'blur'  },
                ],
                unpaid: [
                    { required: true, message: '请填写未付租赁/设备费（元）', trigger: 'blur'  },
                ],
                enterExitState: [
                    { required: true, message: '请选择进退场状态', trigger: 'change'  },
                ],
                specificationModel: [
                    { required: true, message: '请填写规格型号', trigger: 'blur'  },
                ],
            },
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            equipmentSource: state=>state.equip.equipData.equipmentSource, //外协设备来源
            enterExitStatus: state=>state.equip.equipData.enterExitStatus, //进退场状态
            unitMeasure: state => state.equip.equipData.unitMeasure //计量单位
        }),
    },
    created () {
        this.getDicValue()
    },
    mounted () {
    },
    methods: {
        //获取当前使用的字典
        getDicValue () {
            this.$store.dispatch('equip/setUnitMeasur')
        },
        //选择外协单位
        selectUnit () {
            let ids = this.formData.contractId ? [this.formData.contractId] : []
            selectOutsourceUnit(ids, this.userInfo.orgInfo.orgId).then(res=>{
                this.formData.supplierName = res[0].secondPartyName
                this.formData.supplierId = res[0].secondPartyId
                this.formData.contractId = res[0].contractId
            })
        },
        //选择设备
        selectEquip () {
            let json = {
                'categoryId': '00000',
                'isUpdate': false,
                'keyword': ''
            }
            selectDataClient({
                SelectServiceToken: '/facilitybase/equipment/getEquipmentCategoryAll', // 客户端对应方法名
                IsSingleMode: true, // 是否单选 true 单选 false 多选
                SelectedIDs: [], // 已选中id
                InitParamsStr: JSON.stringify(json),    // 接口参数
                Title: '设备基础库' // 标题
            }).then(res=>{
                let item = res[0]
                this.formData.unit = item.unit //计量单位
                this.formData.equipmentName = item.equipmentName  //设备编号
                this.formData.equipmentId = item.equipmentId //设备id
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.outAccountAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/outAccountEdit',
                                query: {
                                    billid: res,
                                    name: 'siteAcceptance'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/outAccountList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
